package zhoug.zui.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;

import zhoug.logging.Logger;
import zhoug.zui.R;
import zhoug.zui.dialog.ZUIBaseDialog;
import zhoug.zui.dialog.ZUIButtonDialog;
import zhoug.zui.dialog.ZUIDialog;
import zhoug.zui.dialog.list.ZUIAbsListDialog;
import zhoug.zui.dialog.list.ZUIMultiSelectListDialog;

import androidx.annotation.AttrRes;
import androidx.annotation.StyleRes;

/**
 * @Author 35574
 * @Date 2020/7/22
 * @Description
 */
public class DialogAttributeParser {
    private static final String TAG = ">>>DialogAttributeParser";
    private static  boolean debug = false;

    public static void setDebug(boolean debug) {
        DialogAttributeParser.debug = debug;
    }

    /**
     * {@link R.styleable#ZUIBaseDialog}中的属性,可以调用此方法解析
     *
     * @param context      Context
     * @param defStyleAttr 全局默认样式的AttrRes,需要在Theme中指定
     * @param defStyleRes  默认的Style
     */
    public static void parseZUIBaseDialogAttrs(ZUIDialog zuiDialog, Context context, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        if (zuiDialog == null) return;
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZUIBaseDialog, defStyleAttr, defStyleRes);
        int corners = a.getDimensionPixelOffset(R.styleable.ZUIBaseDialog_zui_corners, 0);
        boolean fullWidth = a.getBoolean(R.styleable.ZUIBaseDialog_zui_fullWidth, false);
        int titleColor = a.getColor(R.styleable.ZUIBaseDialog_zui_titleColor, ZUIUtils.UN_USED_COLOR);
        float titleSize = a.getDimension(R.styleable.ZUIBaseDialog_zui_titleSize, 0);
        int messageColor = a.getColor(R.styleable.ZUIBaseDialog_zui_messageColor, ZUIUtils.UN_USED_COLOR);
        float messageSize = a.getDimension(R.styleable.ZUIBaseDialog_zui_messageSize, 0);
        int maxHeight = a.getDimensionPixelOffset(R.styleable.ZUIBaseDialog_zui_maxHeight, 0);
        a.recycle();
        if (debug) {
            Logger.d(TAG, "ZUIBaseDialog:corners=" + corners);
            Logger.d(TAG, "ZUIBaseDialog:fullWidth=" + fullWidth);
            Logger.d(TAG, "ZUIBaseDialog:titleColor=" + Integer.toHexString(titleColor));
            Logger.d(TAG, "ZUIBaseDialog:titleSize=" + titleSize);
            Logger.d(TAG, "ZUIBaseDialog:messageColor=" + Integer.toHexString(messageColor));
            Logger.d(TAG, "ZUIBaseDialog:messageSize=" + messageSize);
            Logger.d(TAG, "ZUIBaseDialog:maxHeight=" + maxHeight);
        }
        if (zuiDialog instanceof ZUIBaseDialog) {
            ZUIBaseDialog baseDialog = (ZUIBaseDialog) zuiDialog;
            baseDialog.setCorners(corners);
            baseDialog.setFullWidth(fullWidth);
            baseDialog.setTitleColor(titleColor);
            baseDialog.setTitleSize(titleSize);
            baseDialog.setMessageColor(messageColor);
            baseDialog.setMessageSize(messageSize);
            baseDialog.setMaxHeight(maxHeight);
        }

    }

    /**
     * {@link R.styleable#ZUIButtonDialog}中的属性,可以调用此方法解析
     *
     * @param context      Context
     * @param defStyleAttr 全局默认样式的AttrRes,需要在Theme中指定
     * @param defStyleRes  默认的Style
     */
    public static void parseZUIButtonDialogAttrs(ZUIDialog zuiDialog, Context context, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        if (zuiDialog == null) return;
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZUIButtonDialog, defStyleAttr, defStyleRes);
        int positiveBtnColor = a.getColor(R.styleable.ZUIButtonDialog_zui_positiveBtnColor, ZUIUtils.UN_USED_COLOR);
        float positiveBtnSize = a.getDimension(R.styleable.ZUIButtonDialog_zui_positiveBtnSize, 0);
        int negativeBtnColor = a.getColor(R.styleable.ZUIButtonDialog_zui_negativeBtnColor, ZUIUtils.UN_USED_COLOR);
        float negativeBtnSize = a.getDimension(R.styleable.ZUIButtonDialog_zui_negativeBtnSize, 0);
        a.recycle();
        if (debug) {
            Logger.d(TAG, "ZUIButtonDialog:positiveBtnSize=" + positiveBtnSize);
            Logger.d(TAG, "ZUIButtonDialog:positiveBtnColor=" + Integer.toHexString(positiveBtnColor));
            Logger.d(TAG, "ZUIButtonDialog:negativeBtnSize=" + negativeBtnSize);
            Logger.d(TAG, "ZUIButtonDialog:negativeBtnColor=" + Integer.toHexString(negativeBtnColor));
        }
        if (zuiDialog instanceof ZUIButtonDialog) {
            ZUIButtonDialog baseDialog = (ZUIButtonDialog) zuiDialog;
            baseDialog.setPositiveBtnColor(positiveBtnColor);
            baseDialog.setPositiveBtnSize(positiveBtnSize);
            baseDialog.setNegativeBtnColor(negativeBtnColor);
            baseDialog.setNegativeBtnSize(negativeBtnSize);
        }else if(zuiDialog instanceof ZUIMultiSelectListDialog){
            ZUIMultiSelectListDialog dialog = (ZUIMultiSelectListDialog) zuiDialog;
            dialog.setPositiveBtnColor(positiveBtnColor);
            dialog.setPositiveBtnSize(positiveBtnSize);
            dialog.setNegativeBtnColor(negativeBtnColor);
            dialog.setNegativeBtnSize(negativeBtnSize);
        }

    }

    /**
     * {@link R.styleable#ZUIAbsListDialog}中的属性,可以调用此方法解析
     *
     * @param context      Context
     * @param defStyleAttr 全局默认样式的AttrRes,需要在Theme中指定
     * @param defStyleRes  默认的Style
     */
    public static void parseZUIAbsListDialogAttrs(ZUIDialog zuiDialog, Context context, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        if (zuiDialog == null) return;
        TypedArray a = context.obtainStyledAttributes(null, R.styleable.ZUIAbsListDialog, defStyleAttr, defStyleRes);
        float textSize = a.getDimension(R.styleable.ZUIAbsListDialog_zui_textSize, 0);
        int textColor = a.getColor(R.styleable.ZUIAbsListDialog_zui_textColor, ZUIUtils.UN_USED_COLOR);
        int dividerHeight = a.getDimensionPixelOffset(R.styleable.ZUIAbsListDialog_zui_dividerHeight, 1);
        int dividerColor = a.getColor(R.styleable.ZUIAbsListDialog_zui_dividerColor, Color.parseColor("#dddddd"));
        boolean showDivider = a.getBoolean(R.styleable.ZUIAbsListDialog_zui_showDivider, true);
        a.recycle();
        if (debug) {
            Logger.d(TAG, "ZUIAbsListDialog:textSize=" + textSize);
            Logger.d(TAG, "ZUIAbsListDialog:textColor=" + Integer.toHexString(textColor));
            Logger.d(TAG, "ZUIAbsListDialog:dividerHeight=" + dividerHeight);
            Logger.d(TAG, "ZUIAbsListDialog:dividerColor=" + Integer.toHexString(dividerColor));
            Logger.d(TAG, "ZUIAbsListDialog:showDivider=" +showDivider);
        }
        if (zuiDialog instanceof ZUIAbsListDialog) {
            ZUIAbsListDialog<?> dialog = (ZUIAbsListDialog<?>) zuiDialog;
            dialog.setTextSize(textSize);
            dialog.setTextColor(textColor);
            dialog.setDividerColor(dividerColor);
            dialog.setDividerHeight(dividerHeight);
            dialog.setShowDivider(showDivider);
        }

    }

}
